package org.jivesoftware.smackx;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.Base64;
import org.jivesoftware.smackx.packet.CapsExtension;
import org.jivesoftware.smackx.packet.DataForm;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.provider.CapsExtensionProvider;

public class EntityCapsManager
{
  public static final String HASH_METHOD = "sha-1";
  public static final String HASH_METHOD_CAPS = "SHA-1";
  private static Map<String, DiscoverInfo> caps;
  private static String entityNode = "http://www.igniterealtime.org/projects/smack/";
  private Set<CapsVerListener> capsVerListeners;
  private String currentCapsVersion;
  private Map<String, String> userCaps = new ConcurrentHashMap();

  static
  {
    caps = new ConcurrentHashMap();
    ProviderManager.getInstance().addExtensionProvider("c", "http://jabber.org/protocol/caps", new CapsExtensionProvider());
  }

  public EntityCapsManager()
  {
    this.capsVerListeners = new CopyOnWriteArraySet();
    this.currentCapsVersion = null;
  }

  public static void addDiscoverInfoByNode(String paramString, DiscoverInfo paramDiscoverInfo)
  {
    cleanupDicsoverInfo(paramDiscoverInfo);
    caps.put(paramString, paramDiscoverInfo);
  }

  private static String capsToHash(String paramString)
  {
    String str1;
    String str2;
    try
    {
      str2 = Base64.encodeBytes(MessageDigest.getInstance("SHA-1").digest(paramString.getBytes()));
      str1 = str2;
      return str1;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      str1 = null;
    }
  }

  private static void cleanupDicsoverInfo(DiscoverInfo paramDiscoverInfo)
  {
    paramDiscoverInfo.setFrom(null);
    paramDiscoverInfo.setTo(null);
    paramDiscoverInfo.setPacketID(null);
  }

  private static String formFieldValuesToCaps(Iterator<String> paramIterator)
  {
    Iterator localIterator;
    String str1 = "";
    TreeSet localTreeSet = new TreeSet();
    if (!(paramIterator.hasNext()))
      localIterator = localTreeSet.iterator();
    while (true)
    {
      while (!(localIterator.hasNext()))
      {
        return str1;
        localTreeSet.add((String)paramIterator.next());
      }
      String str2 = (String)localIterator.next();
      str1 = str1 + str2 + "<";
    }
  }

  public static DiscoverInfo getDiscoverInfoByNode(String paramString)
  {
    return ((DiscoverInfo)caps.get(paramString));
  }

  private void notifyCapsVerListeners()
  {
    Iterator localIterator = this.capsVerListeners.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      ((CapsVerListener)localIterator.next()).capsVerUpdated(this.currentCapsVersion);
    }
  }

  public void addCapsVerListener(CapsVerListener paramCapsVerListener)
  {
    this.capsVerListeners.add(paramCapsVerListener);
    if (this.currentCapsVersion != null)
      paramCapsVerListener.capsVerUpdated(this.currentCapsVersion);
  }

  public void addPacketListener(Connection paramConnection)
  {
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketTypeFilter(Presence.class);
    arrayOfPacketFilter[1] = new PacketExtensionFilter("c", "http://jabber.org/protocol/caps");
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    paramConnection.addPacketListener(new CapsPacketListener(this), localAndFilter);
  }

  public void addUserCapsNode(String paramString1, String paramString2)
  {
    if ((paramString1 != null) && (paramString2 != null))
      this.userCaps.put(paramString1, paramString2);
  }

  void calculateEntityCapsVersion(DiscoverInfo paramDiscoverInfo, String paramString1, String paramString2, List<String> paramList, DataForm paramDataForm)
  {
    TreeSet localTreeSet1;
    Iterator localIterator1;
    Iterator localIterator2;
    Object localObject1 = "" + "client/" + paramString1 + "//" + paramString2 + "<";
    monitorenter;
    try
    {
      localTreeSet1 = new TreeSet();
      localIterator1 = paramList.iterator();
      if (!(localIterator1.hasNext()))
      {
        localIterator2 = localTreeSet1.iterator();
        if (localIterator2.hasNext())
          break label233;
        monitorexit;
        label279: label332: label233: monitorenter;
      }
    }
    finally
    {
      TreeSet localTreeSet2;
      Object localObject3;
      Iterator localIterator3;
      Iterator localIterator4;
      try
      {
        while (true)
        {
          localTreeSet2 = new TreeSet(new Comparator(this)
          {
            public int compare(, FormField paramFormField2)
            {
              return paramFormField1.getVariable().compareTo(paramFormField2.getVariable());
            }
          });
          localObject3 = null;
          localIterator3 = paramDataForm.getFields();
          if (localIterator3.hasNext())
            break label279;
          if (localObject3 != null)
            localObject1 = localObject1 + formFieldValuesToCaps(localObject3.getValues());
          localIterator4 = localTreeSet2.iterator();
          if (localIterator4.hasNext())
            break label332;
          monitorexit;
          setCurrentCapsVersion(paramDiscoverInfo, capsToHash((String)localObject1));
          return;
          localTreeSet1.add((String)localIterator1.next());
        }
        localObject2 = finally;
        monitorexit;
      }
      finally
      {
        while (true)
        {
          while (true)
          {
            String str1;
            String str2;
            FormField localFormField1;
            monitorexit;
            throw localObject4;
            localObject3 = localFormField1;
          }
          FormField localFormField2 = (FormField)localIterator4.next();
          String str3 = new StringBuilder(String.valueOf(localObject1)).append(localFormField2.getVariable()).append("<").toString() + formFieldValuesToCaps(localFormField2.getValues());
          localObject1 = str3;
        }
      }
    }
  }

  public String getCapsVersion()
  {
    return this.currentCapsVersion;
  }

  public DiscoverInfo getDiscoverInfoByUser(String paramString)
  {
    DiscoverInfo localDiscoverInfo;
    String str = (String)this.userCaps.get(paramString);
    if (str == null)
      localDiscoverInfo = null;
    while (true)
    {
      return localDiscoverInfo;
      localDiscoverInfo = getDiscoverInfoByNode(str);
    }
  }

  public String getNode()
  {
    return entityNode;
  }

  public String getNodeVersionByUser(String paramString)
  {
    return ((String)this.userCaps.get(paramString));
  }

  public void removeCapsVerListener(CapsVerListener paramCapsVerListener)
  {
    this.capsVerListeners.remove(paramCapsVerListener);
  }

  public void removeUserCapsNode(String paramString)
  {
    this.userCaps.remove(paramString);
  }

  public void setCurrentCapsVersion(DiscoverInfo paramDiscoverInfo, String paramString)
  {
    this.currentCapsVersion = paramString;
    addDiscoverInfoByNode(getNode() + "#" + paramString, paramDiscoverInfo);
    notifyCapsVerListeners();
  }

  public void setNode(String paramString)
  {
    entityNode = paramString;
  }

  class CapsPacketListener
  implements PacketListener
  {
    public void processPacket()
    {
      CapsExtension localCapsExtension = (CapsExtension)paramPacket.getExtension("c", "http://jabber.org/protocol/caps");
      String str1 = localCapsExtension.getNode() + "#" + localCapsExtension.getVersion();
      String str2 = paramPacket.getFrom();
      this.this$0.addUserCapsNode(str2, str1);
    }
  }
}